<?php
namespace app\back\controller;
use think\Controller;
use think\Request;
use think\Db;
use think\session;
class Login extends Controller
{
	/*
	*显示登录页
	*/ 
    public function index()
    {
		return $this->fetch('Login/index');
    }
	public function login()
	{
		$username=input('username');
		$password=input('password');
		if($username){
			$admin=Db::name('admin')->where("username='$username'")->find();
			if($admin){
				if(Db::name('admin')->where(['username'=>$username,'password'=>md5($password)])->find()){
					 session('username',$admin['username']);
					 session('userId',$admin['id']);
					// die("<script>alert('200');</script>");
					$this->redirect('Index/index',3,'页面跳转中~');
				}else{
					// return WSTReturn('密码错误!');
					// die("<script>alert('300');</script>");
					$this->redirect('login/index');  //密码错误
				}
			}else{
				// return WSTReturn('账号错误!');
				// die("<script>alert('400');</script>");
				$this->redirect('login/index');   //账号错误
			}
		}
    }
	//获取原密码
	public function sel_passwd()
	{	
		$password = md5(input('password'));
		$data = Db::name('admin')->where("username='admin'")->find();
		if($data['password']==$password){
			return 1;
		}else{
			return 2;
		}
	}
	//修改原密码
	public function upd_passwd(){
		$confirmpasswd = md5(input('confirmpasswd'));
		//修改原密码
		$arr = array (
			'password' => $confirmpasswd
			);
		$upd_pawd = Db::name('admin')->where("username='admin'")->update($arr);
		if($upd_pawd){
			return 3;
		}else{
			return 4;
		}
	}
	//退出登录
	public function loginout(){
		session('username',null);
		return $this->fetch('Login/index');
	}
}